﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Criterion;

using BOSS.Framework.Search;
using BOSS.Framework.Search.Context;
using BOSS.Domain.Security;
using BOSS.Dao.Security;

namespace BOSS.Dao.NHibernate.Security
{
    /// <summary>
    /// 账号 Dao
    /// </summary>
    public class AccountDao : EntityDao<Account, int>, IAccountDao 
    {
        #region ISearchSupport<Account> 成员

        /// <summary>
        /// 搜索 账号
        /// </summary>
        /// <param name="context"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public IList<Account> SelectBySearch(ISearchContext context, out int totalCount)
        {
            int count = 0;

            string fullName = context.GetFilterTermValueAsString("FullName");
            string employeeId = context.GetFilterTermValueAsString("EmployeeId");

            IList<Account> accounts = this.HibernateTemplate.ExecuteFind<Account>(delegate(ISession session)
            {
                ICriteria criteria = session.CreateCriteria(typeof(Account), "a");

                criteria.Add(Expression.Like("a.FullName", fullName, MatchMode.Anywhere));
                criteria.Add(Expression.Like("a.EmployeeId", employeeId, MatchMode.Anywhere));

                count = criteria.SetProjection(Projections.Count("Id").SetDistinct()).UniqueResult<int>();
                if (count == 0)
                {
                    return new List<Account>();
                }

                criteria.SetProjection(new IProjection[] { null });
                criteria.SetResultTransformer(CriteriaSpecification.DistinctRootEntity);

                return criteria.SetFirstResult(context.PageSize * (context.PageIndex - 1))
                        .SetMaxResults(context.PageSize)
                        .List<Account>();
            });

            totalCount = count;
            return accounts;
        }

        #endregion

        #region IAccountDao 成员

        /// <summary>
        /// 根据登录名查找帐号。找不到则返回 null
        /// </summary>
        /// <param name="loginName"></param>
        /// <returns></returns>
        public Account FindByLoginName(string loginName)
        {
            IList<Account> accounts = this.HibernateTemplate.ExecuteFind<Account>(delegate(ISession session)
            {
                ICriteria criteria = session.CreateCriteria(typeof(Account), "a");
                criteria.Add(Expression.Eq("a.LoginName", loginName));
                return criteria.SetFetchSize(1).List<Account>();
            });

            return accounts.FirstOrDefault();
        }

        /// <summary>
        /// 根据工号查找帐号。找不到则返回 null
        /// </summary>
        /// <param name="employeeId"></param>
        /// <returns></returns>
        public Account FindByEmployeeId(string employeeId)
        {
            IList<Account> accounts = this.HibernateTemplate.ExecuteFind<Account>(delegate(ISession session)
            {
                ICriteria criteria = session.CreateCriteria(typeof(Account), "a");
                criteria.Add(Expression.Eq("a.EmployeeId", employeeId));
                return criteria.SetFetchSize(1).List<Account>();
            });

            return accounts.FirstOrDefault();
        }

        /// <summary>
        /// 查询所有的有效的帐号
        /// </summary>
        /// <returns></returns>
        public IList<Account> SelectAllEnabled()
        {
            return this.HibernateTemplate.Find<Account>("from Account a where a.Disabled = ?", false);
        }

        #endregion
    }
}
